#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
1688商品数据分析脚本
分析爬取到的商品数据，提供统计信息和数据洞察
"""

import json
import statistics
from collections import Counter

def analyze_products(json_file):
    """分析商品数据"""
    with open(json_file, 'r', encoding='utf-8') as f:
        products = json.load(f)
    
    print(f"=== 1688商品数据分析报告 ===")
    print(f"总商品数量: {len(products)}")
    print("=" * 50)
    
    # 价格分析
    prices = []
    for product in products:
        if product.get('price') and product['price'] != 'N/A':
            try:
                price = float(product['price'])
                prices.append(price)
            except ValueError:
                continue
    
    if prices:
        print(f"\n📊 价格分析:")
        print(f"  • 有效价格数据: {len(prices)} 个")
        print(f"  • 最低价格: ¥{min(prices):.2f}")
        print(f"  • 最高价格: ¥{max(prices):.2f}")
        print(f"  • 平均价格: ¥{statistics.mean(prices):.2f}")
        print(f"  • 中位数价格: ¥{statistics.median(prices):.2f}")
        
        # 价格区间分布
        price_ranges = {
            '0-1元': 0,
            '1-10元': 0,
            '10-50元': 0,
            '50-100元': 0,
            '100元以上': 0
        }
        
        for price in prices:
            if price < 1:
                price_ranges['0-1元'] += 1
            elif price < 10:
                price_ranges['1-10元'] += 1
            elif price < 50:
                price_ranges['10-50元'] += 1
            elif price < 100:
                price_ranges['50-100元'] += 1
            else:
                price_ranges['100元以上'] += 1
        
        print(f"\n  价格区间分布:")
        for range_name, count in price_ranges.items():
            percentage = (count / len(prices)) * 100
            print(f"    {range_name}: {count} 个 ({percentage:.1f}%)")
    
    # 销量分析
    sales = []
    for product in products:
        if product.get('sales') and product['sales'] != 'N/A' and product['sales'] != '0':
            try:
                sale = int(product['sales'])
                sales.append(sale)
            except ValueError:
                continue
    
    if sales:
        print(f"\n📈 销量分析:")
        print(f"  • 有销量数据的商品: {len(sales)} 个")
        print(f"  • 最低销量: {min(sales)} 件")
        print(f"  • 最高销量: {max(sales)} 件")
        print(f"  • 平均销量: {statistics.mean(sales):.1f} 件")
        print(f"  • 中位数销量: {statistics.median(sales)} 件")
        
        # 热销商品 (销量前10)
        products_with_sales = [(p['title'], int(p['sales'])) for p in products 
                              if p.get('sales') and p['sales'] != 'N/A' and p['sales'] != '0']
        products_with_sales.sort(key=lambda x: x[1], reverse=True)
        
        print(f"\n  🔥 热销商品TOP10:")
        for i, (title, sale) in enumerate(products_with_sales[:10], 1):
            print(f"    {i}. {title[:40]}{'...' if len(title) > 40 else ''} - {sale}件")
    
    # 关键词分析
    all_titles = [p['title'] for p in products if p.get('title')]
    keywords = []
    for title in all_titles:
        keywords.extend(title.split())
    
    keyword_counter = Counter(keywords)
    print(f"\n🔍 热门关键词TOP10:")
    for keyword, count in keyword_counter.most_common(10):
        percentage = (count / len(all_titles)) * 100
        print(f"  • {keyword}: {count} 次 ({percentage:.1f}%)")
    
    # 数据完整性分析
    print(f"\n📋 数据完整性:")
    title_count = sum(1 for p in products if p.get('title') and p['title'] != 'N/A')
    price_count = sum(1 for p in products if p.get('price') and p['price'] != 'N/A')
    sales_count = sum(1 for p in products if p.get('sales') and p['sales'] != 'N/A' and p['sales'] != '0')
    image_count = sum(1 for p in products if p.get('image') and p['image'] != 'N/A')
    
    print(f"  • 标题完整率: {title_count}/{len(products)} ({(title_count/len(products)*100):.1f}%)")
    print(f"  • 价格完整率: {price_count}/{len(products)} ({(price_count/len(products)*100):.1f}%)")
    print(f"  • 销量完整率: {sales_count}/{len(products)} ({(sales_count/len(products)*100):.1f}%)")
    print(f"  • 图片完整率: {image_count}/{len(products)} ({(image_count/len(products)*100):.1f}%)")
    
    print(f"\n✅ 分析完成！数据文件: {json_file}")

if __name__ == "__main__":
    analyze_products('1688_products.json')